// content.js

// 监听来自 popup 的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
	if (request.action === "getElementsByClassName") 
	{
		if (document.querySelector("[class='right-bar-box short'") && document.querySelector("[class='right-bar-box short'").querySelectorAll("a"))
		{
			var links = document.querySelector("[class='right-bar-box short'").querySelectorAll("a");
			var alink = [];

			links.forEach((link) => { 
				alink.push(link.href); 

				if (request.openwindow) {
					window.open(link.href, '_blank');
				}
     		});
			sendResponse({ success: true,  message: "获取成功", count: links.length, alllinks: alink });
		}
		else
		{
			sendResponse({ success: false, message: "获取失败", count: 0, alllinks: [] });
		}
	}
	
	return true; // 保持消息通道开放以发送异步响应
});